package com.mproject.activity.project;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.mproject.activity.AddProjectActivity;
import com.mproject.activity.R;
import com.mproject.customclass.Category;
import com.mproject.customclass.Project;
import com.mproject.network.IVLE_API_Helper;
import com.mproject.network.ProgressBarManager;

public class Project_Info_Summary extends Activity implements OnClickListener{

	Project project;

	private TextView back_button;
	private TextView save_button;
	private TextView project_title;
	private Spinner project_cat;
	private TextView project_des;

	public static SharedPreferences mypref;

	private String[] category = {"Business / Finance",
			"Computers / Internet",
			"Cultures / Community",
			"Entertainment / Arts",
			"Family / Home",
			"Games",
			"Halls of Residence",
			"Hobbies / Craft",
			"Lifestyles",
			"Music",
			"Others",
			"Projects",
			"Relationships / Romance",
			"School / Education",
			"Science / Technology",
			"Second Life",
	"Sports / Recreation"};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.project_info_summary); 

		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.project_info_summary, null); 
		this.setContentView(viewToLoad); 



	}

	public void onResume() {
		super.onResume();
		mypref=getSharedPreferences("My_Prefs",Activity.MODE_WORLD_WRITEABLE);
		back_button = (TextView) findViewById(R.id.project_info_summary_back);
		back_button.setOnClickListener(this);
		save_button = (TextView)findViewById(R.id.project_info_summary_save);
		save_button.setOnClickListener(this);
		project_title = (TextView)findViewById(R.id.project_info_set_title);
		project_cat = (Spinner)findViewById(R.id.project_info_set_category);
		project_des = (TextView)findViewById(R.id.project_info_set_description);

		Bundle extras=getIntent().getExtras();
		project=(Project) extras.getSerializable("project");

		//set project
		project_title.setText(project.getTitle());
		project_des.setText(project.getDescription());
		for (int i = 0; i < category.length; i++) {
			if (project.getCategory().equals(category[i])) {
				project_cat.setSelection(i);
				break;
			}
		}

		//check current user is manager to set permission
		if (project.isManager()) {
			project_title.setEnabled(true);
			project_cat.setEnabled(true);
			project_des.setEnabled(true);
			save_button.setVisibility(View.VISIBLE);
		}
		else {
			project_title.setEnabled(false);
			project_cat.setEnabled(false);
			project_des.setEnabled(false);
			save_button.setVisibility(View.INVISIBLE);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == back_button) {
			finish();
		}
		if (v == save_button) {
			if (project_title.getText().toString().equals("")) {
				AlertDialog.Builder builder = new AlertDialog.Builder(Project_Info_Summary.this.getParent());
				builder.setMessage("Title cannot be empty!")
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}

				});
				AlertDialog alert = builder.create();
				alert.show();
			}
			else {
				ProgressBarManager manager=new ProgressBarManager(Project_Info_Summary.this.getParent())
				{

					protected void onPreExecute() {

						Log.v("manager"," in pre execute");

						dialog.setMessage("Loading please wait...");
						dialog.setIndeterminate(true);

						// dialog.setTitle("Manager");
						dialog.show();
					}

					protected Boolean doInBackground(String... params) 
					{

						try {
							//get category
							List<Category> category_list = IVLE_API_Helper.getCategory(mypref.getString("IVLE_Token", ""));
							String category_id = "";
							for (Category category : category_list) {
								if (project_cat.getSelectedItem().toString().equals(category.getCategory_name())) {
									category_id = category.getCategory_id();
									Log.e("Category",category.getCategory_name());
									Log.e("Category",category.getCategory_id());
									break;
								}
							}
							//create project
							IVLE_API_Helper.updateProjet(mypref.getString("IVLE_Token", ""),project.getID(), project_title.getText().toString(), project_des.getText().toString(), category_id,project.getOpenDate(),project.getExpiryDate());
							SharedPreferences.Editor editor=mypref.edit();
							editor.putString("project_title",project_title.getText().toString());
							editor.putString("project_des",project_des.getText().toString());
							editor.putString("project_cat",project_cat.getSelectedItem().toString());
							editor.commit();

							return true;
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							return false;
						}


					}
					protected void onPostExecute(Boolean result) {
						this.getDialog().dismiss();
						if (result == false) {
							AlertDialog.Builder builder = new AlertDialog.Builder(Project_Info_Summary.this.getParent());
							builder.setMessage("No internet connection")
							.setNegativeButton("OK", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int id) {
									dialog.cancel();
								}

							});
							AlertDialog alert = builder.create();
							alert.show();
						}
						else {

							Project_TabView.welcome_name.setText(project_title.getText().toString());

						}
					}
				};
				manager.execute();
			}

		}

	}
}
